﻿// This code is released under the BSD license.
namespace PugLib.Threading
{
    using System;
    using System.Threading;

    #region ThreadHelper

    /// <summary>
    /// Provides methods for handling threads
    /// </summary>
    [Serializable]
    public class ThreadHelper
    {
        /// <summary>
        /// Gets or sets a value indicating the scheduling priority of a thread.
        /// </summary>
        /// <value>One of the <see creaf="System.Threading.ThreadPriority"/> values. The default value is <c>Normal</c>.</value>
        public virtual ThreadPriority Priority
        {
            get { return Thread.CurrentThread.Priority; }
            set { Thread.CurrentThread.Priority = value; }
        }

        /// <summary>
        /// Gets or sets the name of the thread.
        /// </summary>
        /// <value>A string containing the name of the thread, or null if no name was set.</value>
        public virtual string Name
        {
            get { return Thread.CurrentThread.Name; }
            set { Thread.CurrentThread.Name = value; }
        }

        /// <summary>
        /// Suspends the current thread for a specified time.
        /// </summary>
        /// <param name="millisecondsTimeout">The number of milliseconds for which the thread is blocked. Specify zero
        ///(0) to indicate that this thread should be suspended to allow other waiting
        ///threads to execute. Specify <see cref="System.Threading.Timeout.Infinite"/> to block the
        ///thread indefinitely.</param>
        public virtual void Sleep(int millisecondsTimeout)
        {
            Thread.Sleep(millisecondsTimeout);
        }

        /// <summary>
        /// Blocks the current thread for a specified time.
        /// </summary>
        /// <param name="timeout">A System.TimeSpan set to the amount of time for which the thread is blocked.
        ///Specify zero to indicate that this thread should be suspended to allow other
        ///waiting threads to execute. Specify System.Threading.Timeout.Infinite to
        ///block the thread indefinitely.</param>
        public virtual void Sleep(TimeSpan timeout)
        {
            Thread.Sleep(timeout);
        }

        /// <summary>
        /// Queues a method for execution. The method executes when a thread pool thread becomes available.
        /// </summary>
        /// <param name="callback">A <see cref="System.Threading.WaitCallback"/> representing the method to be executed.</param>
        /// <returns>true if the method is successfully queued; System.OutOfMemoryException is thrown if the work item could not be queued.</returns>
        public virtual bool QueueUserWorkItem(WaitCallback callback)
        {
            return ThreadPool.QueueUserWorkItem(callback);
        }

        /// <summary>
        /// Queues a method for execution, and specifies an object containing data to
        /// be used by the method. The method executes when a thread pool thread becomes
        /// available.
        /// </summary>
        /// <param name="callback">A <see cref="System.Threading.WaitCallback"/> representing the method to execute.</param>
        /// <param name="state">An object containing data to be used by the method.</param>
        /// <returns>true if the method is successfully queued; System.OutOfMemoryException is thrown if the work item could not be queued.</returns>
        public virtual bool QueueUserWorkItem(WaitCallback callback, object state)
        {
            return ThreadPool.QueueUserWorkItem(callback, state);
        }
    }

    #endregion ThreadHelper
}